package api

import (
	db "fiberAmisAdmin/config"
	"fmt"
	"github.com/gofiber/fiber/v2"
	"github.com/gofiber/fiber/v2/log"
	"golang.org/x/net/html"
	"net/http"
	"strings"
)

type IndexController struct {
	BaseController
}

func Test(c *fiber.Ctx) error {
	data := fiber.Map{
		"message": "Hello, this is an example JSON response!",
	}
	return c.JSON(data)
}

func (tc *IndexController) Test2(ctx *fiber.Ctx) error {
	var result []map[string]interface{}
	//err := SQLServerDB.Table("MS_000001.dbo.User_Info").Find(&result)
	err := db.MySQLDB.Table("admin_operation_log").Find(&result).Error
	if err != nil {
		fmt.Println("err:", err)
		return tc.returnError(ctx, fiber.StatusOK, "Error")
	}
	return tc.returnSuccess(ctx, fiber.StatusOK, "Success", result)
}

func GetText(ctx *fiber.Ctx) error {
	resp, err := http.Get("http://127.0.0.1:8888/informationView?category1=%E4%BF%A1%E6%81%AF%E7%AE%A1%E7%90%86")
	if err != nil {
		log.Fatal(err)
	}
	defer resp.Body.Close()

	// 使用HTML解析器解析HTML
	doc, err := html.Parse(resp.Body)
	if err != nil {
		log.Fatal(err)
	}

	// 从HTML中提取所有文本内容
	text := extractText(doc)

	// 返回提取的文本内容
	return ctx.SendString(text)
}
func extractText(n *html.Node) string {
	var text string
	if n.Type == html.TextNode {
		text = n.Data
	}
	for c := n.FirstChild; c != nil; c = c.NextSibling {
		text += extractText(c)
	}
	return strings.TrimSpace(text)
}
